#include "ResetFactoryDialog.h"
#include "ui_ResetFactoryDialog.h"
#include "ResetFactoryWorker.h"
#include <QThread>
#include "Mcu.h"

ResetFactoryDialog *ResetFactoryDialog::getInstance()
{
    static ResetFactoryDialog singleton{};
    return &singleton;
}

void ResetFactoryDialog::exeStartSlot()
{
    ui->info->setText(tr("将删除所有影像参数和患者信息，是否执行？"));
    ui->progress->setRange(0, 1);
    ui->progress->setValue(0);
    ui->no->setEnabled(true);
    ui->yes->setEnabled(true);
}

void ResetFactoryDialog::exeErrorSlot()
{
    ui->info->setText(tr("执行失败"));
    ui->progress->setRange(0, 1);
    ui->progress->setValue(0);
    ui->no->setEnabled(true);
    ui->yes->setEnabled(true);
}

void ResetFactoryDialog::exeCompleteSlot()
{
    ui->info->setText(tr("执行完成，请重启系统..."));
    ui->progress->setRange(0, 1);
    ui->progress->setValue(1);
    ui->no->setEnabled(false);
    ui->yes->setEnabled(false);
//    QThread::msleep(2000);
    Mcu::getInstance()->touchSoundBeepMessage();
//    system("reboot");
}

ResetFactoryDialog::ResetFactoryDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ResetFactoryDialog)
{
    ui->setupUi(this);
    QObject::connect(this, &ResetFactoryDialog::startWorkSignal, ResetFactoryWorker::getInstance(), &ResetFactoryWorker::startWorkSlot);
    QObject::connect(ResetFactoryWorker::getInstance(), &ResetFactoryWorker::completeWorkSignal, this, &ResetFactoryDialog::exeCompleteSlot);
    QObject::connect(ResetFactoryWorker::getInstance(), &ResetFactoryWorker::exeErrorSignal, this, &ResetFactoryDialog::exeErrorSlot);
}

ResetFactoryDialog::~ResetFactoryDialog()
{
    delete ui;
}

void ResetFactoryDialog::on_yes_clicked()
{
    ui->info->setText(tr("正在执行..."));
    ui->progress->setRange(0, 0);
    ui->no->setEnabled(false);
    ui->yes->setEnabled(false);

    emit startWorkSignal();
}

void ResetFactoryDialog::on_no_clicked()
{
    ui->progress->setRange(0, 1);
    ui->progress->setValue(0);
    hide();
}
